//index.js
const app = getApp()
const apis = require('../../utils/api').apis;

Page({
  data: {
    navData: [],
    dataArr: [],
    nav_active: 0,
    bannerArr: [],
    menuItemList: [],
    learnSchedule: null,
    nowTime: null,
    tabScrollTop: 0,
    tabFixed: false,
    nav_id: 0,
    nav_type: 0,
    userInfo: {
      userType: 'user'
    },

  },

  onLoad: function (options) {
    var timestamp = Date.parse(new Date());
    timestamp = this.formatTimes(timestamp);
    let userType = wx.getStorageSync('userInfo') ? wx.getStorageSync('userInfo').userType : 'user';
    this.setData({
      nowTime: timestamp,
      'userInfo.userType': userType
    })
    console.log(this.data.userInfo.userType, this.data.nowTime)
    wx.showLoading({
      title: '加载中',
    })
    // apis.menuitemQueryHomeTop({}).then(res => {
    //   this.setData({
    //     navData: res.data,
    //     nav_id: res.data[0].id,
    //     nav_type: res.data[0].type
    //   })
    //   this.getData(this.data.nav_id, this.data.nav_type)
    //   // wx.hideLoading()
    // })
    wx.removeStorageSync('jumpUrl'); //删除banner跳转路径
  },
  onShow() {
    console.log(this.data.nav_active)

    var timestamp = Date.parse(new Date());
    timestamp = this.formatTimes(timestamp);
    let userType = wx.getStorageSync('userInfo') ? wx.getStorageSync('userInfo').userType : 'user';
    this.setData({
      nowTime: timestamp,
      'userInfo.userType': userType
    })
    console.log(this.data.userInfo.userType)
    apis.menuitemQueryHomeTop({}).then(res => {
      this.setData({
        navData: res.data,
      })
      console.log('top', this.data.nav_id)
      for(var i=0;i<this.data.navData.length;i++){
        console.log(this.data.nav_id,  this.data.navData[i].id)
        if (this.data.nav_id == this.data.navData[i].id) {
          this.getData(this.data.nav_id, this.data.nav_type)
          return
        }
   

      }
      this.setData({
        nav_id: res.data[0].id,
        nav_type: res.data[0].type,
        nav_active: 0
      })
      this.getData(this.data.navData[0].id, this.data.navData[0].type)
    })
    // wx.removeStorageSync('jumpUrl'); //删除banner跳转路径

  },

  searchWord() {
    wx.navigateTo({
      url: '/home/pages/search/index',
    })
  },
  switchTap(e) {
    this.setData({
      nav_active: e.currentTarget.dataset.index,
      nav_id: e.currentTarget.dataset.id,
      nav_type: e.currentTarget.dataset.type
    })
    this.getData(this.data.nav_id, this.data.nav_type)
  },
  getData(id, type) {
    wx.showLoading({
      title: '加载中',
    })
    apis.menuitemQueryMenuItem({
      relation: id,
      relationType: type
    }).then(res => {
      console.log('首页数据', res.data)
      this.setData({
        dataArr: res.data,
        bannerArr: res.data.bannerList.length > 1 ? res.data.bannerList : res.data.bannerList,
        learnSchedule: Object.keys(res.data.learnSchedule != null).length === 0 ? null : res.data.learnSchedule,
        menuItemList: res.data.menuItemList
      })
      wx.hideLoading()
    })
  },
  more() {
    wx.switchTab({
      url: '../curriculum/index',
    })
  },
  goDetail(e) {
    let id = e.currentTarget.dataset.id;
    console.log(id)
    wx.navigateTo({
      url: '../../curriculum/pages/course/index?id=' + id,
    })
  },
  goLive(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../../curriculum/pages/course_zhibo/index?id=' + id,
    })
  },
  bindZhengshu() {
    var token = wx.getStorageSync('Xd_Token');
    if (token == '' || token == null || token == undefined) {
      wx.navigateTo({
        url: '/login/pages/login/index',
      })
      return
    }
    wx.navigateTo({
      url: '/home/pages/index_zhengshu/index',
    })
  },
  bindVip() {
    var token = wx.getStorageSync('Xd_Token');
    if (token == '' || token == null || token == undefined) {
      wx.navigateTo({
        url: '/login/pages/login/index',
      })
      return
    }
    wx.navigateTo({
      url: '/home/pages/index_vip1/index'
    })
  },
  formatTimes(row) {
    if (!row) return '无';
    const date = new Date(row),
      year = date.getFullYear(),
      month = date.getMonth() > 9 ? date.getMonth() + 1 : `0${date.getMonth() + 1}`,
      day = date.getDate() > 9 ? date.getDate() : `0${date.getDate()}`,
      hh = date.getHours() > 9 ? date.getHours() : `0${date.getHours()}`,
      mm = date.getMinutes() > 9 ? date.getMinutes() : `0${date.getMinutes()}`,
      ss = date.getSeconds() > 9 ? date.getSeconds() : `0${date.getSeconds()}`;
    return `${year}-${month}-${day} ${hh}:${mm}:${ss}`;
  },

  onReady: function () {
    var that = this;
    var query = wx.createSelectorQuery()
    query.select('#nav').boundingClientRect(function (res) {
      that.setData({
        tabScrollTop: res.top
      })
    }).exec()
  },
  onPageScroll: function (e) { // 获取滚动条当前位置
    console.log(e.scrollTop)
    if (e.scrollTop > this.data.tabScrollTop) {
      this.setData({
        tabFixed: true
      })
    } else {
      this.setData({
        tabFixed: false
      })
    }
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
})